
const axios = require("axios");
const http = axios.create();

axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response;
}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});

/* axios.default.interceptors.request.use(
    config => {
      let url = config.url;
      // get参数进行百分比编码
      if (config.method === "get" && config.params) {
        url += "?";
        let keys = Object.keys(config.params);
        for (let key of keys) {
          url += `${key}=${encodeURIComponent(config.params[key])}&`;
        }
        url = url.substring(0, url.length - 1);
        config.params = {};
      }
      config.url = url;
      return config;
    },
    function(error) {
      return Promise.reject(error);
    }
  ); */


const get = function (url, config) {
  return http.get(url, config).then((response) => {
    const { data, status, statusText } = response;
    if (status === 200 || status === 304) {
      return data;
    } else {
      throw new Error(statusText);
    }
  })
}

// const searchActor = 'actor'
/* function(){
  return actor
} */

export const getRecommendMusic = function () {
  return get("/mock/recommendMusic.json");
}
export const getRecommend = function () {
  return get("/mock/recommend.json");
}

export const getSearchData = function () {
  // console.log(this.$route)
  return get(`/ting?from=android&version=5.6.5.0&method=baidu.ting.search.catalogSug&format=json&query=七里香`)
}
